package com.cz.nongyetongv5.activity;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.bean.AssistSupervise;
import com.cz.nongyetongv5.bean.AssistSuperviseSaleDetail;
import com.cz.nongyetongv5.bean.AssistSuperviseUseDetail;
import com.cz.nongyetongv5.bean.NzdSalePic;
import com.cz.nongyetongv5.bean.NzdUsePic;
import com.cz.nongyetongv5.bean.PatrolPic;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class AssistSuperviseSaleDetailActivity extends FragmentActivity implements View.OnClickListener {

    private RelativeLayout rl_back;
    private TextView tv_title;


    private TextView tv_name;
    private TextView tv_product;
    private TextView tv_count;
    private TextView tv_sales;

    private RecyclerView lv_patrol_pic;
    private RelativeLayout rl_patrol_pic;


    private AssistSuperviseSaleDetail nzdSale;
    private List<NzdSalePic> nzdSalePics;
    private PhotoAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_assist_supervise_sale);
        rl_back = findViewById(R.id.rl_back);
        rl_back.setOnClickListener(this);
        tv_title = findViewById(R.id.tv_title);
        tv_title.setText("农产品销售详情");
        nzdSale = (AssistSuperviseSaleDetail) getIntent().getSerializableExtra("nzdSale");

        tv_name = findViewById(R.id.tv_name);
        tv_product = findViewById(R.id.tv_product);

        tv_count = findViewById(R.id.tv_count);
        tv_sales = findViewById(R.id.tv_sales);

        lv_patrol_pic = findViewById(R.id.lv_patrol_pic);
        rl_patrol_pic = findViewById(R.id.rl_patrol_pic);


        if (nzdSale != null) {
            if (nzdSale.getPhoto() == null || nzdSale.getPhoto().isEmpty()) {
                rl_patrol_pic.setVisibility(View.GONE);
            } else {
                rl_patrol_pic.setVisibility(View.VISIBLE);
                nzdSalePics = nzdSale.getPhoto();
                adapter = new PhotoAdapter();
                lv_patrol_pic.setLayoutManager(new GridLayoutManager(AssistSuperviseSaleDetailActivity.this, 3));
                lv_patrol_pic.setAdapter(adapter);
                adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        List<PatrolPic> picBeenList = new ArrayList<>();
                        for (NzdSalePic pic :
                                nzdSale.getPhoto()) {
                            picBeenList.add(new PatrolPic(pic.getAttach_url()));
                        }
                        Intent intent = new Intent(AssistSuperviseSaleDetailActivity.this, NzdPicDetailActivity.class);
                        intent.putExtra("position", position);
                        intent.putExtra("photos", (Serializable) picBeenList);
                        startActivity(intent);
                    }
                });
            }
            tv_name.setText(nzdSale.getSupervise_name());
            tv_product.setText(nzdSale.getProduct_name());
            tv_count.setText(nzdSale.getNumber() + nzdSale.getNumber_unit_name());
            tv_sales.setText(nzdSale.getSale_direction());
        }

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_back:
                finish();
                break;

            default:
                break;
        }
    }

    public class PhotoAdapter extends BaseQuickAdapter<NzdSalePic, BaseViewHolder> {
        public PhotoAdapter() {
            super(R.layout.product_pic_item, nzdSalePics);
        }

        @Override
        protected void convert(BaseViewHolder helper, NzdSalePic item) {
            Glide.with(mContext)
                    .load(item.getAttach_url()).placeholder(R.drawable.loading)
                    .into((ImageView) helper.getView(R.id.iv_iv));

        }
    }

}
